package cn.wjdiankong.ijkplayer.manager;

import java.util.ArrayList;
import java.util.List;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import cn.wjdiankong.ijkplayer.view.IRenderView;
import cn.wjdiankong.ijkplayer.view.IjkVideoView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.utils.Logger;
import cn.wjdiankong.levideo.utils.NetworkUtil;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.view.ProgressWheel;
import cn.wjdiankong.levideo.view.seekbar.DiscreteSeekBar;
import cn.wjdiankong.levideo.view.seekbar.DiscreteSeekBar.OnProgressChangeListener;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnCompletionListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

@SuppressLint("DefaultLocale")
public class PlayerView {

	/**
	 * 可能会剪裁,保持原视频的大小，显示在中心,当原视频的大小超过view的大小超过部分裁剪处理
	 */
	public static final String SCALETYPE_FITPARENT="fitParent";
	/**
	 * 可能会剪裁,等比例放大视频，直到填满View为止,超过View的部分作裁剪处理
	 */
	public static final String SCALETYPE_FILLPARENT="fillParent";
	/**
	 * 将视频的内容完整居中显示，如果视频大于view,则按比例缩视频直到完全显示在view中
	 */
	public static final String SCALETYPE_WRAPCONTENT="wrapContent";
	/**
	 * 不剪裁,非等比例拉伸画面填满整个View
	 */
	public static final String SCALETYPE_FITXY="fitXY";
	/**
	 * 不剪裁,非等比例拉伸画面到16:9,并完全显示在View中
	 */
	public static final String SCALETYPE_16_9="16:9";
	/**
	 * 不剪裁,非等比例拉伸画面到4:3,并完全显示在View中
	 */
	public static final String SCALETYPE_4_3="4:3";

	/**
	 * 打印日志的TAG
	 */
	private static final String TAG = PlayerView.class.getSimpleName();
	/**
	 * 全局上下文
	 */
	private final Context mContext;
	/**
	 * 依附的容器Activity
	 */
	private final Activity mActivity;
	/**
	 * Activity界面的中布局的查询器
	 */
	private final LayoutQuery query;
	/**
	 * 原生的Ijkplayer
	 */
	private final IjkVideoView videoView;
	/**
	 * 播放器整个界面
	 */
	private final View rl_box;
	/**
	 * 播放器顶部控制bar
	 */
	private final View ll_topbar;
	/**
	 * 播放器底部控制bar
	 */
	private final View ll_bottombar;
	/**
	 * 视频bottonbar的播放按钮
	 */
	private final ImageView iv_bar_player;
	/**
	 * 播放器封面，播放前的封面或者缩列图
	 */
	private final ImageView iv_trumb;
	/**
	 * 视频返回按钮
	 */
	private final ImageView iv_back;
	/**
	 * 视频菜单按钮
	 */
	private final ImageView iv_menu;
	/**
	 * 视频中间的播放按钮
	 */
	private final ImageView iv_player;
	/**
	 * 视频加载速度
	 */
	private final TextView tv_speed;
	/**
	 * 视频播放进度条
	 */
	private final DiscreteSeekBar seekBar;
	/**
	 * 码流列表
	 */
	private List<VideoijkBean> listVideos = new ArrayList<VideoijkBean>();

	/**
	 * loading列表
	 */
	private ProgressWheel mSmileyLoadingView;

	/**
	 * 当前状态
	 */
	private int status = PlayStateParams.STATE_IDLE;
	/**
	 * 当前播放位置
	 */
	private int currentPosition;
	/**
	 * 滑动进度条得到的新位置，和当前播放位置是有区别的,newPosition =0也会调用设置的，故初始化值为-1
	 */
	private long newPosition = -1;
	/**
	 * 视频旋转的角度，默认只有0,90.270分别对应向上、向左、向右三个方向
	 */
	private int rotation = 0;
	/**
	 * 视频显示比例,默认保持原视频的大小
	 */
	private int currentShowType = PlayStateParams.fitparent;
	/**
	 * 播放总时长
	 */
	private long duration;
	/**
	 * 记录播放器竖屏时的高度
	 */
	private final int initHeight;
	/**
	 * 当前播放地址
	 */
	private String currentUrl;
	/**
	 * 记录进行后台时的播放状态0为播放，1为暂停
	 */
	private int bgState;
	/**
	 * 自动重连的时间
	 */
	private int autoConnectTime = 5000;
	/**
	 * 第三方so是否支持，默认不支持，true为支持
	 */
	private boolean playerSupport;
	/**
	 * 是否是直播 默认为非直播，true为直播false为点播，根据isLive()方法前缀rtmp或者后缀.m3u8判断得出的为直播，比较片面，有好的建议欢迎交流
	 */
	private boolean isLive;
	/**
	 * 是否显示控制面板，默认为隐藏，true为显示false为隐藏
	 */
	private boolean isShowControlPanl;
	/**
	 * 禁止触摸，默认可以触摸，true为禁止false为可触摸
	 */
	private boolean isForbidTouch;
	/**
	 * 禁止收起控制面板，默认可以收起，true为禁止false为可触摸
	 */
	private boolean isForbidHideControlPanl;
	/**
	 * 当前是否切换视频流，默认为否，true是切换视频流，false没有切换
	 */
	private boolean isHasSwitchStream;
	/**
	 * 是否在拖动进度条中，默认为停止拖动，true为在拖动中，false为停止拖动
	 */
	private boolean isDragging;
	/**
	 * 播放的时候是否需要网络提示，默认显示网络提示，true为显示网络提示，false不显示网络提示
	 */
	private boolean isGNetWork = true;

	/**
	 * 是否只有全屏，默认非全屏，true为全屏，false为非全屏
	 */
	private boolean isOnlyFullScreen;
	/**
	 * 是否出错停止播放，默认是出错停止播放，true出错停止播放,false为用户点击停止播放
	 */
	private boolean isErrorStop = true;
	/**
	 * 是否是竖屏，默认为竖屏，true为竖屏，false为横屏
	 */
	private boolean isPortrait = true;
	/**
	 * 是否隐藏中间播放按钮，默认不隐藏，true为隐藏，false为不隐藏
	 */
	private boolean isHideCenterPlayer;
	/**
	 * 是否自动重连，默认5秒重连，true为重连，false为不重连
	 */
	private boolean isAutoReConnect = true;
	/**
	 * 是否隐藏topbar，true为隐藏，false为不隐藏
	 */
	private boolean isHideTopBar;
	/**
	 * 是否隐藏bottonbar，true为隐藏，false为不隐藏
	 */
	private boolean isHideBottonBar;
	/**
	 * 同步进度
	 */
	private static final int MESSAGE_SHOW_PROGRESS = 1;
	/**
	 * 设置新位置
	 */
	private static final int MESSAGE_SEEK_NEW_POSITION = 3;
	/**
	 * 隐藏提示的box
	 */
	private static final int MESSAGE_HIDE_CENTER_BOX = 4;
	/**
	 * 重新播放
	 */
	private static final int MESSAGE_RESTART_PLAY = 5;

	private int curVideoPosition = 0;
	private boolean isShowBar = true;
	
	private boolean isCyclePlay = true;
	
	/**
	 * 消息处理
	 */
	private Handler mHandler = new Handler(Looper.getMainLooper()) {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			/**滑动完成，隐藏滑动提示的box*/
			case MESSAGE_HIDE_CENTER_BOX:
				query.id(R.id.app_video_volume_box).gone();
				query.id(R.id.app_video_brightness_box).gone();
				query.id(R.id.app_video_fastForward_box).gone();
				break;
				/**滑动完成，设置播放进度*/
			case MESSAGE_SEEK_NEW_POSITION:
				if (!isLive && newPosition >= 0) {
					videoView.seekTo((int) newPosition);
					newPosition = -1;
				}
				break;
				/**滑动中，同步播放进度*/
			case MESSAGE_SHOW_PROGRESS:
				long pos = syncProgress();
				if (!isDragging && isShowControlPanl) {
					msg = obtainMessage(MESSAGE_SHOW_PROGRESS);
					sendMessageDelayed(msg, 1000 - (pos % 1000));
					updatePausePlay();
				}
				break;
				/**重新去播放*/
			case MESSAGE_RESTART_PLAY:
				status = PlayStateParams.STATE_ERROR;
				startPlay();
				updatePausePlay();
				break;
			}
		}
	};

	/**========================================视频的监听方法==============================================*/

	/**
	 * 控制面板收起或者显示的轮询监听
	 */
	private AutoPlayRunnable mAutoPlayRunnable = new AutoPlayRunnable();
	/**
	 * Activity界面方向监听
	 */
	private final OrientationEventListener orientationEventListener;
	/**
	 * 控制面板显示或隐藏监听
	 */
	private OnControlPanelVisibilityChangeListener onControlPanelVisibilityChangeListener;
	/**
	 * 视频封面显示监听
	 */
	private OnShowThumbnailListener mOnShowThumbnailListener;
	/**
	 * 视频的返回键监听
	 */
	private OnPlayerBackListener mPlayerBack;
	/**
	 * 视频播放时信息回调
	 */
	private IMediaPlayer.OnInfoListener onInfoListener;

	/**
	 * 点击事件监听
	 */
	private final View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v.getId() == R.id.app_video_play || v.getId() == R.id.play_icon) {
				/**视频播放和暂停*/
				if (videoView.isPlaying()) {
					if (isLive) {
						videoView.stopPlayback();
					} else {
						pausePlay();
					}
					if(clickListener != null){
						clickListener.click(VideoClickListener.CLICK_PAUSE);
					}
				} else {
					if(clickListener != null){
						clickListener.click(VideoClickListener.CLICK_PLAY);
					}
					startPlay();
					if (videoView.isPlaying()) {
						/**ijkplayer内部的监听没有回调，只能手动修改状态*/
						status = PlayStateParams.STATE_PREPARING;
						hideStatusUI();
					}
				}
				updatePausePlay();
			} else if (v.getId() == R.id.app_video_finish) {
				/**返回*/
				if (!isOnlyFullScreen && !isPortrait) {
					mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
				} else {
					if (mPlayerBack != null) {
						mPlayerBack.onPlayerBack();
					} else {
						mActivity.finish();
					}
				}
				if(clickListener != null){
					clickListener.click(VideoClickListener.CLICK_FINISH);
				}
			} else if (v.getId() == R.id.app_video_netTie_icon) {
				/**使用移动网络提示继续播放*/
				isGNetWork = false;
				hideStatusUI();
				startPlay();
				updatePausePlay();
			} else if (v.getId() == R.id.app_video_replay) {
				/**重新播放*/
				status = PlayStateParams.STATE_ERROR;
				hideStatusUI();
				videoView.seekTo(0);
				videoView.start();
				updatePausePlay();
				if(clickListener != null){
					clickListener.click(VideoClickListener.CLICK_REPLAY);
				}
			}
		}
	};

	private final OnProgressChangeListener mSeekListeners = new OnProgressChangeListener(){

		@Override
		public void onProgressChanged(DiscreteSeekBar seekBar, int progress, boolean fromUser) {
			if (!fromUser) {
				/**不是用户拖动的，自动播放滑动的情况*/
				return;
			} else {
				long duration = getDuration();
				int position = (int) ((duration * progress * 1.0) / 1000);
				String time = generateTime(position);
				seekBar.setValue(time);
				query.id(R.id.app_video_currentTime).text(time);
			}
		}

		@Override
		public void onStartTrackingTouch(DiscreteSeekBar seekBar) {
			isDragging = true;
			mHandler.removeMessages(MESSAGE_SHOW_PROGRESS);
		}

		@Override
		public void onStopTrackingTouch(DiscreteSeekBar seekBar) {
			long duration = getDuration();
			videoView.seekTo((int) ((duration * seekBar.getProgress() * 1.0) / 1000));
			mHandler.removeMessages(MESSAGE_SHOW_PROGRESS);
			isDragging = false;
			mHandler.sendEmptyMessageDelayed(MESSAGE_SHOW_PROGRESS, 1000);
			if(clickListener != null){
				clickListener.click(VideoClickListener.SLIDE_PROGRESSBAR);
			}
			
		}
	};

	/**
	 * 更新播放、暂停和停止按钮
	 */
	private void updatePausePlay() {
		if (videoView.isPlaying()) {
			iv_bar_player.setImageResource(R.drawable.detail_icon_pause_normal);
		} else {
			iv_bar_player.setImageResource(R.drawable.detail_icon_play_normal);
		}
	}
	
	public boolean isPlaying(){
		return videoView.isPlaying();
	}

	public void setCyclePlay(boolean cyclePlay){
		this.isCyclePlay = cyclePlay;
	}

	/**
	 * ========================================视频的监听方法==============================================
	 */

	/**
	 * 保留旧的调用方法
	 */
	public PlayerView(Activity activity) {
		this(activity, null);
	}

	/**
	 * 新的调用方法，适用非Activity中使用PlayerView，例如fragment、holder中使用
	 */
	public PlayerView(Activity activity, View rootView) {
		this.mActivity = activity;
		this.mContext = activity;
		try {
			IjkMediaPlayer.loadLibrariesOnce(null);
			IjkMediaPlayer.native_profileBegin("libijkplayer.so");
			playerSupport = true;
		} catch (Throwable e) {
			Log.e(TAG, "loadLibraries error", e);
		}

		try {
			int e = Settings.System.getInt(this.mContext.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
			float progress = 1.0F * (float) e / 255.0F;
			WindowManager.LayoutParams layout = this.mActivity.getWindow().getAttributes();
			layout.screenBrightness = progress;
			mActivity.getWindow().setAttributes(layout);
		} catch (Settings.SettingNotFoundException var7) {
			var7.printStackTrace();
		}
		if (rootView == null) {
			query = new LayoutQuery(mActivity);
			rl_box = mActivity.findViewById(R.id.app_video_box);
			videoView = (IjkVideoView) mActivity.findViewById(R.id.video_view);
			mSmileyLoadingView = (ProgressWheel)mActivity.findViewById(R.id.loading_view);
			ll_topbar = mActivity.findViewById(R.id.app_video_top_box);
			ll_bottombar = mActivity.findViewById(R.id.ll_bottom_bar);
			iv_trumb = (ImageView) mActivity.findViewById(R.id.iv_trumb);
			iv_back = (ImageView) mActivity.findViewById(R.id.app_video_finish);
			iv_menu = (ImageView) mActivity.findViewById(R.id.app_video_menu);
			iv_player = (ImageView) mActivity.findViewById(R.id.play_icon);
			tv_speed = (TextView) mActivity.findViewById(R.id.app_video_speed);
			seekBar = (DiscreteSeekBar) mActivity.findViewById(R.id.app_video_seekBar);
			iv_bar_player = (ImageView) mActivity.findViewById(R.id.app_video_play);
		} else {
			query = new LayoutQuery(mActivity, rootView);
			rl_box = rootView.findViewById(R.id.app_video_box);
			videoView = (IjkVideoView) rootView.findViewById(R.id.video_view);
			mSmileyLoadingView = (ProgressWheel)mActivity.findViewById(R.id.loading_view);
			ll_topbar = rootView.findViewById(R.id.app_video_top_box);
			ll_bottombar = rootView.findViewById(R.id.ll_bottom_bar);
			iv_trumb = (ImageView) rootView.findViewById(R.id.iv_trumb);
			iv_back = (ImageView) rootView.findViewById(R.id.app_video_finish);
			iv_menu = (ImageView) rootView.findViewById(R.id.app_video_menu);
			iv_player = (ImageView) rootView.findViewById(R.id.play_icon);
			tv_speed = (TextView) rootView.findViewById(R.id.app_video_speed);
			seekBar = (DiscreteSeekBar) rootView.findViewById(R.id.app_video_seekBar);
			iv_bar_player = (ImageView) rootView.findViewById(R.id.app_video_play);
		}

		seekBar.setUpdate(false);
		seekBar.setValueColor(0xFFFFFFFF);
		seekBar.setOnProgressChangeListener(mSeekListeners);

		iv_player.setOnClickListener(onClickListener);
		iv_bar_player.setOnClickListener(onClickListener);
		iv_back.setOnClickListener(onClickListener);
		query.id(R.id.app_video_netTie_icon).clicked(onClickListener);
		query.id(R.id.app_video_replay).clicked(onClickListener);
		videoView.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
			@Override
			public boolean onInfo(IMediaPlayer mp, int what, int extra) {
				statusChange(what);
				if (onInfoListener != null) {
					onInfoListener.onInfo(mp, what, extra);
				}
				return true;
			}
		});
		videoView.setOnCompletionListener(new OnCompletionListener() {
			@Override
			public void onCompletion(IMediaPlayer mp) {
				if (onInfoListener != null) {
					onInfoListener.onInfo(mp, PlayStateParams.STATE_COMPLETED, 0);
				}
				if(isCyclePlay){
					videoView.seekTo(0);
					videoView.start();
				}else{
					showStatus("播放完成，点击重播");
				}
			}
		});
		videoView.setOnErrorListener(new OnErrorListener(){
			@Override
			public boolean onError(IMediaPlayer mp, int what, int extra) {
				statusChange(what);
				if (onInfoListener != null) {
					onInfoListener.onInfo(mp, PlayStateParams.MEDIA_INFO_VIDEO_INTERRUPT, 0);
				}
				showStatus("播放错误");
				if(!NetworkUtil.isNetworkAvailable(mActivity)){
					UIUtil.showShortHintToast(mActivity, "网络错误,请查看网络!");
				}
				return false;
			}
		});
		orientationEventListener = new OrientationEventListener(mActivity) {
			@Override
			public void onOrientationChanged(int orientation) {
				if (orientation >= 0 && orientation <= 30 || orientation >= 330 || (orientation >= 150 && orientation <= 210)) {
					//竖屏
					if (isPortrait) {
						mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
						orientationEventListener.disable();
					}
				} else if ((orientation >= 90 && orientation <= 120) || (orientation >= 240 && orientation <= 300)) {
					if (!isPortrait) {
						mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
						orientationEventListener.disable();
					}
				}
			}
		};
		if (isOnlyFullScreen) {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		isPortrait = (getScreenOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		initHeight = rl_box.getLayoutParams().height;
		hideAll();
		if (!playerSupport) {
			showStatus(mActivity.getResources().getString(R.string.not_support));
		} else {
			query.id(R.id.ll_bg).visible();
		}
	}
	
	public void showBarView(){
		ObjectAnimator animatorT = ObjectAnimator.ofFloat(ll_topbar, "y", 
				ll_topbar.getTop() - ll_topbar.getHeight(),ll_topbar.getTop());
		animatorT.setDuration(300);
		animatorT.start();
		if(!isHideBottonBar){
			ObjectAnimator animatorB = ObjectAnimator.ofFloat(ll_bottombar, "y", 
					ll_bottombar.getBottom(), ll_bottombar.getBottom() - ll_bottombar.getHeight());
			animatorB.setDuration(300);
			animatorB.start();
		}else{
			ll_bottombar.setVisibility(View.GONE);
		}
		isShowBar = true;
		autoHideBarView();
	}
	
	private AutoHideBarRunnable hideBarRunable = new AutoHideBarRunnable();

	public void hideBarView(){
		videoView.removeCallbacks(hideBarRunable);
		ObjectAnimator animatorT = ObjectAnimator.ofFloat(ll_topbar, "y", 
				ll_topbar.getTop(),ll_topbar.getTop() - ll_topbar.getHeight());
		animatorT.setDuration(300);
		animatorT.start();
		if(!isHideBottonBar){
			ObjectAnimator animatorB = ObjectAnimator.ofFloat(ll_bottombar, "y", 
					ll_bottombar.getBottom() - ll_bottombar.getHeight(), ll_bottombar.getBottom());
			animatorB.setDuration(300);
			animatorB.start();
		}
		isShowBar = false;
	}
	
	private void autoHideBarView(){
		videoView.postDelayed(hideBarRunable, 4000);
	}
	
	public void toggleBarView(){
		if(isShowBar){
			hideBarView();
		}else{
			showBarView();
		}
	}

	/**==========================================Activity生命周期方法回调=============================*/
	public PlayerView onPause() {
		bgState = (videoView.isPlaying() ? 0 : 1);
		getCurrentPosition();
		videoView.pause();
		return this;
	}

	public PlayerView onResume() {
		videoView.resume();
		if (isLive) {
			videoView.seekTo(0);
		} else {
			videoView.seekTo(currentPosition);
		}
		if (bgState == 0) {

		} else {
			pausePlay();
		}
		return this;
	}

	public PlayerView onDestroy() {
		orientationEventListener.disable();
		mHandler.removeMessages(MESSAGE_RESTART_PLAY);
		mHandler.removeMessages(MESSAGE_SEEK_NEW_POSITION);
		videoView.stopPlayback();
		return this;
	}

	public PlayerView onConfigurationChanged(final Configuration newConfig) {
		isPortrait = newConfig.orientation == Configuration.ORIENTATION_PORTRAIT;
		doOnConfigurationChanged(isPortrait);
		return this;
	}

	public boolean onBackPressed() {
		if (!isOnlyFullScreen && getScreenOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			return true;
		}
		return false;
	}

	/**
	 * 显示缩略图
	 */
	public PlayerView showThumbnail(OnShowThumbnailListener onShowThumbnailListener) {
		this.mOnShowThumbnailListener = onShowThumbnailListener;
		if (mOnShowThumbnailListener != null && iv_trumb != null) {
			mOnShowThumbnailListener.onShowThumbnail(iv_trumb);
		}
		return this;
	}

	/**
	 * 设置播放信息监听回调
	 */
	public PlayerView setOnInfoListener(IMediaPlayer.OnInfoListener onInfoListener) {
		this.onInfoListener = onInfoListener;
		return this;
	}

	/**
	 * 设置播放器中的返回键监听
	 */
	public PlayerView setPlayerBackListener(OnPlayerBackListener listener) {
		this.mPlayerBack = listener;
		return this;
	}

	/**
	 * 设置控制面板显示隐藏监听
	 */
	public PlayerView setOnControlPanelVisibilityChangListenter(OnControlPanelVisibilityChangeListener listener) {
		this.onControlPanelVisibilityChangeListener = listener;
		return this;
	}

	/**
	 * 百分比显示切换
	 */
	public PlayerView toggleAspectRatio() {
		if (videoView != null) {
			videoView.toggleAspectRatio();
		}
		return this;
	}

	/**
	 * 设置播放区域拉伸类型
	 */
	public PlayerView setScaleType(int showType) {
		currentShowType = showType;
		videoView.setAspectRatio(currentShowType);
		return this;
	}

	/**
	 * 旋转角度
	 */
	public PlayerView setPlayerRotation() {
		if (rotation == 0) {
			rotation = 90;
		} else if (rotation == 90) {
			rotation = 270;
		} else if (rotation == 270) {
			rotation = 0;
		}
		setPlayerRotation(rotation);
		return this;
	}

	/**
	 * 旋转指定角度
	 */
	public PlayerView setPlayerRotation(int rotation) {
		if (videoView != null) {
			videoView.setAspectRatio(currentShowType);
		}
		return this;
	}

	/**
	 * 设置播放地址
	 * 包括视频清晰度列表
	 * 对应地址列表
	 */
	public PlayerView setPlaySource(List<VideoijkBean> list, int position) {
		listVideos.clear();
		if (list != null && list.size() > 0) {
			listVideos.addAll(list);
			switchStream(position);
		}
		return this;
	}

	/**
	 * 设置播放地址
	 * 单个视频VideoijkBean
	 */
	public PlayerView setPlaySource(VideoijkBean videoijkBean) {
		listVideos.clear();
		if (videoijkBean != null) {
			listVideos.add(videoijkBean);
			switchStream(0);
		}
		return this;
	}

	/**
	 * 设置播放地址
	 * 单个视频地址时
	 * 带流名称
	 */
	public PlayerView setPlaySource(String title, String url) {
		VideoijkBean mVideoijkBean = new VideoijkBean();
		mVideoijkBean.url = url;
		setPlaySource(mVideoijkBean);
		return this;
	}

	/**
	 * 自动播放
	 */
	public PlayerView autoPlay(String title, String path) {
		setPlaySource(title, path);
		startPlay();
		return this;
	}

	/**
	 * 开始播放
	 */
	@SuppressWarnings("static-access")
	public PlayerView startPlay() {
		hideLoading();
		if (isLive) {
			videoView.setVideoPath(currentUrl);
			videoView.seekTo(0);
		} else {
			if (isHasSwitchStream || status == PlayStateParams.STATE_ERROR) {
				//换源之后声音可播，画面卡住，主要是渲染问题，目前只是提供了软解方式，后期提供设置方式
				videoView.setRender(videoView.RENDER_TEXTURE_VIEW);
				videoView.setVideoPath(currentUrl);
				videoView.seekTo(currentPosition);
				isHasSwitchStream = false;
			}
		}
		hideStatusUI();
		if (playerSupport) {
			showLoading();
			videoView.start();
		} else {
			showStatus(mActivity.getResources().getString(R.string.not_support));
		}
		showBarView();
		return this;
	}

	/**
	 * 设置视频名称
	 */
	public PlayerView setTitle(String title) {
		query.id(R.id.app_video_title).text(title);
		return this;
	}

	/**
	 * 选择要播放的流
	 */
	public PlayerView switchStream(int index) {
		if (listVideos.size() > index && index >= 0) {
			currentUrl = listVideos.get(index).url;
			setTitle(listVideos.get(index).title);
			curVideoPosition = index;
			currentPosition = 0;
			videoView.seekTo(0);
			isHasSwitchStream = true;
		}
		return this;
	}

	/**
	 * 暂停播放
	 */
	public PlayerView pausePlay() {
		status = PlayStateParams.STATE_PAUSED;
		getCurrentPosition();
		videoView.pause();
		return this;
	}

	/**
	 * 停止播放
	 */
	public PlayerView stopPlay() {
		videoView.stopPlayback();
		isErrorStop = true;
		if (mHandler != null) {
			mHandler.removeMessages(MESSAGE_RESTART_PLAY);
		}
		return this;
	}

	/**
	 * 设置播放位置
	 */
	public PlayerView seekTo(int playtime) {
		videoView.seekTo(playtime);
		return this;
	}

	/**
	 * 获取当前播放位置
	 */
	public int getCurrentPosition() {
		if (!isLive) {
			currentPosition = videoView.getCurrentPosition();
		} else {
			/**直播*/
			currentPosition = -1;
		}
		return currentPosition;
	}

	/**
	 * 获取视频播放总时长
	 */
	public long getDuration() {
		duration = videoView.getDuration();
		return duration;
	}

	/**
	 * 设置2/3/4/5G和WiFi网络类型提示，
	 *
	 * @param isGNetWork true为进行2/3/4/5G网络类型提示
	 *                   false 不进行网络类型提示
	 */
	public PlayerView setNetWorkTypeTie(boolean isGNetWork) {
		this.isGNetWork = isGNetWork;
		return this;
	}

	/**
	 * 是否仅仅为全屏
	 */
	public PlayerView setOnlyFullScreen(boolean isFull) {
		this.isOnlyFullScreen = isFull;
		tryFullScreen(isOnlyFullScreen);
		if (isOnlyFullScreen) {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		} else {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
		}
		return this;
	}

	/**
	 * 设置是否禁止隐藏bar
	 */
	public PlayerView setForbidHideControlPanl(boolean flag) {
		this.isForbidHideControlPanl = flag;
		return this;
	}

	/**
	 * 当前播放的是否是直播
	 */
	public boolean isLive() {
		if (currentUrl != null
				&& (currentUrl.startsWith("rtmp://")
						|| (currentUrl.startsWith("http://") && currentUrl.endsWith(".m3u8"))
						|| (currentUrl.startsWith("http://") && currentUrl.endsWith(".flv")))) {
			isLive = true;
		} else {
			isLive = false;
		}
		return isLive;
	}

	/**
	 * 是否禁止触摸
	 */
	public PlayerView forbidTouch(boolean forbidTouch) {
		this.isForbidTouch = forbidTouch;
		return this;
	}

	/**
	 * 隐藏所有状态界面
	 */
	public PlayerView hideAllUI() {
		if (query != null) {
			hideAll();
		}
		return this;
	}

	/**
	 * 获取顶部控制barview
	 */
	public View getTopBarView() {
		return ll_topbar;
	}

	/**
	 * 获取底部控制barview
	 */
	public View getBottonBarView() {
		return ll_bottombar;
	}

	/**
	 * 获取返回view
	 */
	public ImageView getBackView() {
		return iv_back;
	}

	/**
	 * 获取菜单view
	 */
	public ImageView getMenuView() {
		return iv_menu;
	}

	/**
	 * 获取中间的播放view
	 */
	public ImageView getPlayerView() {
		return iv_player;
	}

	/**
	 * 隐藏返回键，true隐藏，false为显示
	 */
	public PlayerView hideBack(boolean isHide) {
		iv_back.setVisibility(isHide ? View.GONE : View.VISIBLE);
		return this;
	}

	/**
	 * 隐藏菜单键，true隐藏，false为显示
	 */
	public PlayerView hideMenu(boolean isHide) {
		iv_menu.setVisibility(isHide ? View.GONE : View.VISIBLE);
		return this;
	}

	/**
	 * 隐藏中间播放按钮,ture为隐藏，false为不做隐藏处理，但不是显示
	 */
	public PlayerView hideCenterPlayer(boolean isHide) {
		isHideCenterPlayer = isHide;
		iv_player.setVisibility(isHideCenterPlayer ? View.GONE : View.VISIBLE);
		return this;
	}

	/**
	 * 是否隐藏topbar，true为隐藏，false为不隐藏，但不一定是显示
	 */
	public PlayerView hideHideTopBar(boolean isHide) {
		isHideTopBar = isHide;
		ll_topbar.setVisibility(isHideTopBar ? View.GONE : View.VISIBLE);
		return this;
	}

	/**
	 * 是否隐藏bottonbar，true为隐藏，false为不隐藏，但不一定是显示
	 */
	public PlayerView hideBottonBar(boolean isHide) {
		isHideBottonBar = isHide;
		ll_bottombar.setVisibility(isHideBottonBar ? View.GONE : View.VISIBLE);
		return this;
	}

	/**
	 * 是否隐藏上下bar，true为隐藏，false为不隐藏，但不一定是显示
	 */
	public PlayerView hideControlPanl(boolean isHide) {
		hideBottonBar(isHide);
		hideHideTopBar(isHide);
		return this;
	}

	/**
	 * 设置自动重连的模式或者重连时间，isAuto true 出错重连，false出错不重连，connectTime重连的时间
	 */
	public PlayerView setAutoReConnect(boolean isAuto, int connectTime) {
		this.isAutoReConnect = isAuto;
		this.autoConnectTime = connectTime;
		return this;
	}

	/**
	 * 显示或隐藏操作面板
	 */
	public PlayerView operatorPanl() {
		isShowControlPanl = !isShowControlPanl;
		Logger.log("hide bottom:"+isHideBottonBar);
		if (isShowControlPanl) {
			ll_topbar.setVisibility(isHideTopBar ? View.GONE : View.VISIBLE);
			ll_bottombar.setVisibility(isHideBottonBar ? View.GONE : View.VISIBLE);
			if (isLive) {
				query.id(R.id.app_video_process_panl).invisible();
			} else {
				query.id(R.id.app_video_process_panl).visible();
			}
			if (onControlPanelVisibilityChangeListener != null) {
				onControlPanelVisibilityChangeListener.change(true);
			}
			/**显示面板的时候再根据状态显示播放按钮*/
			if (status == PlayStateParams.STATE_PLAYING
					|| status == PlayStateParams.STATE_PREPARED
					|| status == PlayStateParams.STATE_PREPARING
					|| status == PlayStateParams.STATE_PAUSED) {
				if (isHideCenterPlayer) {
					iv_player.setVisibility(View.GONE);
				} else {
					iv_player.setVisibility(isLive ? View.GONE : View.VISIBLE);
				}
			} else {
				iv_player.setVisibility(View.GONE);
			}
			updatePausePlay();
			mHandler.sendEmptyMessage(MESSAGE_SHOW_PROGRESS);
			mAutoPlayRunnable.start();
		} else {
			if (isHideTopBar) {
				ll_topbar.setVisibility(View.GONE);
			} else {
				ll_topbar.setVisibility(isForbidHideControlPanl ? View.VISIBLE : View.GONE);

			}
			if (isHideBottonBar) {
				ll_bottombar.setVisibility(View.GONE);
			} else {
				ll_bottombar.setVisibility(isForbidHideControlPanl ? View.VISIBLE : View.GONE);

			}
			if (!isLive && status == PlayStateParams.STATE_PAUSED && !videoView.isPlaying()) {
				if (isHideCenterPlayer) {
					iv_player.setVisibility(View.GONE);
				} else {
					/**暂停时一直显示按钮*/
					iv_player.setVisibility(View.VISIBLE);
				}
			} else {
				iv_player.setVisibility(View.GONE);
			}
			mHandler.removeMessages(MESSAGE_SHOW_PROGRESS);
			if (onControlPanelVisibilityChangeListener != null) {
				onControlPanelVisibilityChangeListener.change(false);
			}
			mAutoPlayRunnable.stop();
		}
		return this;
	}

	/**
	 * 全屏切换
	 */
	public PlayerView toggleFullScreen() {
		if (getScreenOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		} else {
			mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		return this;
	}

	/**
	 * 设置显示加载网速或者隐藏
	 */
	public PlayerView setShowSpeed(boolean isShow) {
		tv_speed.setVisibility(isShow ? View.VISIBLE : View.GONE);
		return this;
	}

	/**
	 * 获取界面方向
	 */
	public int getScreenOrientation() {
		int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
		DisplayMetrics dm = new DisplayMetrics();
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		int orientation;
		if ((rotation == Surface.ROTATION_0
				|| rotation == Surface.ROTATION_180) && height > width ||
				(rotation == Surface.ROTATION_90
				|| rotation == Surface.ROTATION_270) && width > height) {
			switch (rotation) {
			case Surface.ROTATION_0:
				orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
				break;
			case Surface.ROTATION_90:
				orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
				break;
			case Surface.ROTATION_180:
				orientation =
				ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
				break;
			case Surface.ROTATION_270:
				orientation =
				ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
				break;
			default:
				orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
				break;
			}
		}
		// if the device's natural orientation is landscape or if the device
		// is square:
			else {
				switch (rotation) {
				case Surface.ROTATION_0:
					orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
					break;
				case Surface.ROTATION_90:
					orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
					break;
				case Surface.ROTATION_180:
					orientation =
					ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
					break;
				case Surface.ROTATION_270:
					orientation =
					ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
					break;
				default:
					orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
					break;
				}
			}

		return orientation;
	}

	/**
	 * 状态改变同步UI
	 */
	private void statusChange(int newStatus) {
		if (newStatus == PlayStateParams.STATE_COMPLETED) {
			status = PlayStateParams.STATE_COMPLETED;
			currentPosition = 0;
			hideAll();
		} else if (newStatus == PlayStateParams.STATE_PREPARING
				|| newStatus == PlayStateParams.MEDIA_INFO_BUFFERING_START) {
			status = PlayStateParams.STATE_PREPARING;
			/**视频缓冲*/
			hideStatusUI();
		} else if (newStatus == PlayStateParams.MEDIA_INFO_VIDEO_RENDERING_START
				|| newStatus == PlayStateParams.STATE_PLAYING
				|| newStatus == PlayStateParams.STATE_PREPARED
				|| newStatus == PlayStateParams.MEDIA_INFO_BUFFERING_END
				|| newStatus == PlayStateParams.STATE_PAUSED) {
			if (status == PlayStateParams.STATE_PAUSED) {
				status = PlayStateParams.STATE_PAUSED;
			} else {
				status = PlayStateParams.STATE_PLAYING;
			}
			/**视频缓冲结束后隐藏缩列图*/
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					hideStatusUI();
					/**显示控制bar*/
					isShowControlPanl = false;
					if (!isForbidTouch) {
						operatorPanl();
					}
					/**延迟0.5秒隐藏视频封面隐藏*/
					query.id(R.id.ll_bg).gone();
				}
			}, 500);
		} else if (newStatus == PlayStateParams.MEDIA_INFO_VIDEO_INTERRUPT) {
			/**直播停止推流*/
			status = PlayStateParams.STATE_ERROR;
			if (!(isGNetWork &&
					(NetworkUtils.getNetworkType(mContext) == 4
					|| NetworkUtils.getNetworkType(mContext) == 5
					|| NetworkUtils.getNetworkType(mContext) == 6))) {
				hideAll();
				if (isLive) {
					showStatus("获取不到直播源");
				} else {
					showStatus(mActivity.getResources().getString(R.string.small_problem));
				}
				/**5秒尝试重连*/
				if (!isErrorStop && isAutoReConnect) {
					mHandler.sendEmptyMessageDelayed(MESSAGE_RESTART_PLAY, autoConnectTime);
				}
			} else {
				query.id(R.id.app_video_netTie).visible();
			}

		} else if (newStatus == PlayStateParams.STATE_ERROR
				|| newStatus == PlayStateParams.MEDIA_INFO_UNKNOWN
				|| newStatus == PlayStateParams.MEDIA_ERROR_IO
				|| newStatus == PlayStateParams.MEDIA_ERROR_MALFORMED
				|| newStatus == PlayStateParams.MEDIA_ERROR_UNSUPPORTED
				|| newStatus == PlayStateParams.MEDIA_ERROR_TIMED_OUT
				|| newStatus == PlayStateParams.MEDIA_ERROR_SERVER_DIED) {
			status = PlayStateParams.STATE_ERROR;
			if (!(isGNetWork && (NetworkUtils.getNetworkType(mContext) == 4 || NetworkUtils.getNetworkType(mContext) == 5 || NetworkUtils.getNetworkType(mContext) == 6))) {
				hideStatusUI();
				if (isLive) {
					showStatus(mActivity.getResources().getString(R.string.small_problem));
				} else {
					showStatus(mActivity.getResources().getString(R.string.small_problem));
				}
				/**5秒尝试重连*/
				if (!isErrorStop && isAutoReConnect) {
					mHandler.sendEmptyMessageDelayed(MESSAGE_RESTART_PLAY, autoConnectTime);
				}
			} else {
				query.id(R.id.app_video_netTie).visible();
			}
		}
	}

	/**
	 * 显示视频播放状态提示
	 */
	private void showStatus(String statusText) {
		query.id(R.id.app_video_replay).visible();
		query.id(R.id.app_video_status_text).text(statusText);
	}

	/**
	 * 界面方向改变是刷新界面
	 */
	private void doOnConfigurationChanged(final boolean portrait) {
		if (videoView != null && !isOnlyFullScreen) {
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					tryFullScreen(!portrait);
					if (portrait) {
						query.id(R.id.app_video_box).height(initHeight, false);
					} else {
						int heightPixels = mActivity.getResources().getDisplayMetrics().heightPixels;
						int widthPixels = mActivity.getResources().getDisplayMetrics().widthPixels;
						query.id(R.id.app_video_box).height(Math.min(heightPixels, widthPixels), false);
					}
				}
			});
			orientationEventListener.enable();
		}
	}


	/**
	 * 设置界面方向
	 */
	private void setFullScreen(boolean fullScreen) {
		if (mActivity != null) {

			WindowManager.LayoutParams attrs = mActivity.getWindow().getAttributes();
			if (fullScreen) {
				attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
				mActivity.getWindow().setAttributes(attrs);
				mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			} else {
				attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
				mActivity.getWindow().setAttributes(attrs);
				mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
			}
		}

	}

	/**
	 * 设置界面方向带隐藏actionbar
	 */
	private void tryFullScreen(boolean fullScreen) {
		setFullScreen(fullScreen);
	}

	/**
	 * <pre>
	 *     fitParent:可能会剪裁,保持原视频的大小，显示在中心,当原视频的大小超过view的大小超过部分裁剪处理
	 *     fillParent:可能会剪裁,等比例放大视频，直到填满View为止,超过View的部分作裁剪处理
	 *     wrapContent:将视频的内容完整居中显示，如果视频大于view,则按比例缩视频直到完全显示在view中
	 *     fitXY:不剪裁,非等比例拉伸画面填满整个View
	 *     16:9:不剪裁,非等比例拉伸画面到16:9,并完全显示在View中
	 *     4:3:不剪裁,非等比例拉伸画面到4:3,并完全显示在View中
	 * </pre>
	 * @param scaleType
	 */
	public void setScaleType(String scaleType) {
		if (SCALETYPE_FITPARENT.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_ASPECT_FIT_PARENT);
		}else if (SCALETYPE_FILLPARENT.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_ASPECT_FILL_PARENT);
		}else if (SCALETYPE_WRAPCONTENT.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_ASPECT_WRAP_CONTENT);
		}else if (SCALETYPE_FITXY.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_MATCH_PARENT);
		}else if (SCALETYPE_16_9.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_16_9_FIT_PARENT);
		}else if (SCALETYPE_4_3.equals(scaleType)) {
			videoView.setAspectRatio(IRenderView.AR_4_3_FIT_PARENT);
		}
	}

	private void showLoading(){
		query.id(R.id.app_video_loading).visible();
		if(mSmileyLoadingView != null){
			mSmileyLoadingView.setVisibility(View.VISIBLE);
		}
	}

	private void hideLoading(){
		query.id(R.id.app_video_loading).gone();
		if(mSmileyLoadingView != null){
			mSmileyLoadingView.setVisibility(View.GONE);
		}
	}


	/**
	 * 隐藏状态界面
	 */
	private void hideStatusUI() {
		iv_player.setVisibility(View.GONE);
		query.id(R.id.app_video_replay).gone();
		query.id(R.id.app_video_netTie).gone();
		hideLoading();
		if (onControlPanelVisibilityChangeListener != null) {
			onControlPanelVisibilityChangeListener.change(false);
		}
	}

	/**
	 * 隐藏所有界面
	 */
	private void hideAll() {
		if (!isForbidHideControlPanl) {
			ll_topbar.setVisibility(View.GONE);
			ll_bottombar.setVisibility(View.GONE);
		}
		hideStatusUI();
	}


	/**
	 * 同步进度
	 */
	private long syncProgress() {
		if (isDragging) {
			return 0;
		}
		long position = videoView.getCurrentPosition();
		long duration = videoView.getDuration();
		if (seekBar != null) {
			if (duration > 0) {
				long pos = 1000L * position / duration;
				seekBar.setProgress((int) pos);
			}
		}
		query.id(R.id.app_video_currentTime).text(generateTime(position));
		query.id(R.id.app_video_endTime).text(generateTime(duration));
		return position;
	}

	/**
	 * 时长格式化显示
	 */
	private String generateTime(long time) {
		int totalSeconds = (int) (time / 1000);
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60) % 60;
		int hours = totalSeconds / 3600;
		return hours > 0 ? String.format("%02d:%02d:%02d", hours, minutes, seconds) : String.format("%02d:%02d", minutes, seconds);
	}
	
	public int getCurVideoPosition(){
		return curVideoPosition;
	}
	
	public List<VideoijkBean> getVideoList(){
		return listVideos;
	}

	/**
	 * 收起控制面板轮询，默认5秒无操作，收起控制面板，
	 */
	private class AutoPlayRunnable implements Runnable {
		private int AUTO_PLAY_INTERVAL = 5000;
		private boolean mShouldAutoPlay;

		/**
		 * 五秒无操作，收起控制面板
		 */
		public AutoPlayRunnable() {
			mShouldAutoPlay = false;
		}

		public void start() {
			if (!mShouldAutoPlay) {
				mShouldAutoPlay = true;
				mHandler.removeCallbacks(this);
				mHandler.postDelayed(this, AUTO_PLAY_INTERVAL);
			}
		}

		public void stop() {
			if (mShouldAutoPlay) {
				mHandler.removeCallbacks(this);
				mShouldAutoPlay = false;
			}
		}

		@Override
		public void run() {
			if (mShouldAutoPlay) {
				mHandler.removeCallbacks(this);
				if (!isForbidTouch && !isShowControlPanl) {
					operatorPanl();
				}
			}
		}
	}
	
	private class AutoHideBarRunnable implements Runnable{
		@Override
		public void run() {
			hideBarView();
		}
	}
	
	public interface VideoClickListener{
		public final static int CLICK_PLAY = 1;
		public final static int CLICK_PAUSE = 2;
		public final static int CLICK_REPLAY = 3;
		public final static int SLIDE_PROGRESSBAR = 4;
		public final static int CLICK_FINISH = 5;
		public final static int CLICK_MENU = 6;
		void click(int type);
	}
	
	private VideoClickListener clickListener;
	public void setVideoClickListener(VideoClickListener listener){
		clickListener = listener;
	}

}
